<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- <div class="box">
            <span>hellow</span>
        </div> -->
    </div>
    <script>
        var obj = {
            name: "zhamgsn"
        }
        console.log(obj["name"]);
        // 虚拟dom，就是一个描述真实dom的js对象

        var app1 = {
            type: "div",
            props: {
                class: "box",
                children: [{
                    type: "span",
                    props: {
                        children: ["hellow"]
                    }
                }]
            }

        }

        function render(jsx, root) { // 渲染的方法
            function createElement(jsx, parent) { // 创建元素的方法
                let cur = null; // 这个变量用于承接创建出来的dom节点
                if (typeof (jsx) === "string") {
                    cur = document.createTextNode(jsx)
                } else {
                    let {
                        type,
                        props
                    } = jsx
                    console.log(type, props);
                    cur = document.createElement(type)
                    parent.append(cur)
                    console.log(Object.keys(props))
                    Object.keys(props).forEach((item, index) => {
                        if (item === "class") {
                            cur.setAttribute('class', props[item])
                        } else if (item === "children") {
                            if (Array.isArray(props[item])) {
                                props[item].map((val) => {
                                    createElement(val, cur)
                                })
                            }
                        }
                    })
                }
                parent.append(cur)
            }
            createElement(jsx, root)
        }
        render(app1, app)
    </script>
</body>

</html>